package com.adamfass.jmetest;

import java.util.logging.Logger;

import com.jme.app.AbstractGame;
import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.image.Texture;
import com.jme.input.InputSystem;
import com.jme.input.joystick.JoystickInput;
import com.jme.intersection.BoundingCollisionResults;
import com.jme.intersection.CollisionResults;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.AlphaState;
import com.jme.scene.state.LightState;
import com.jme.scene.state.MaterialState;
import com.jme.scene.state.TextureState;
import com.jme.scene.state.ZBufferState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import com.jmex.font2d.Font2D;
import com.jmex.font2d.Text2D;

public class Test1 extends SimpleGame {
  private static final Logger LOG = Logger.getLogger(Test1.class.getName());
  
  Quad character1;
  Quad character2;
  Node collisionNode;
  Text2D text;
  
  JoystickInput input;
  
  protected void simpleInitGame() {
    collisionNode = new Node("Collision");
    rootNode.attachChild(collisionNode);
    
    final LightState lightState = 
      DisplaySystem.getDisplaySystem().getRenderer().createLightState();
    lightState.setGlobalAmbient(new ColorRGBA(1, 1, 1, 1));
    lightState.setEnabled(true);
    
    final AlphaState alphaState = display.getRenderer().createAlphaState();
    alphaState.setBlendEnabled(true);
    alphaState.setSrcFunction(AlphaState.SB_SRC_ALPHA);
    alphaState.setDstFunction(AlphaState.SB_ONE_MINUS_SRC_ALPHA);
    alphaState.setTestEnabled(true);
    alphaState.setTestFunction(AlphaState.TF_GREATER);
    
    final MaterialState materialState = 
      display.getRenderer().createMaterialState();
    materialState.setAmbient(new ColorRGBA(1, 1, 1, 1));
    materialState.setMaterialFace(MaterialState.MF_FRONT_AND_BACK);
        
    character1 = new Quad("Character 1", 16, 24);
    character1.setLocalTranslation(-10, 0, 0);
    final TextureState textureState1 = 
      display.getRenderer().createTextureState();
    textureState1.setEnabled(true);
    final Texture texture1 = TextureManager.loadTexture(
            Test1.class.getClassLoader().getResource(
                      "com/adamfass/jmetest/blue.png"), 
            Texture.MM_NEAREST, Texture.FM_NEAREST);
    textureState1.setTexture(texture1);
    character1.setRenderState(textureState1);
    character1.setRenderState(alphaState);
    character1.setRenderState(materialState);
    character1.setLightCombineMode(LightState.REPLACE);
    character1.setRenderState(lightState);
    character1.setModelBound(new BoundingBox());
    character1.updateModelBound();
    
    character2 = new Quad("Character 2", 16, 24);
    character2.setLocalTranslation(10, 0, 0);
    
    final TextureState textureState2 = 
      display.getRenderer().createTextureState();
    textureState2.setEnabled(true);
    final Texture texture2 = TextureManager.loadTexture(
            Test1.class.getClassLoader().getResource(
                      "com/adamfass/jmetest/red.png"), 
            Texture.MM_NEAREST, Texture.FM_NEAREST);
    textureState2.setTexture(texture2);
    character2.setRenderState(textureState2);
    character2.setRenderState(alphaState);
    character2.setRenderState(materialState);
    character2.setLightCombineMode(LightState.REPLACE);
    character2.setRenderState(lightState);
    character2.setModelBound(new BoundingBox());
    character2.updateModelBound();
    
    collisionNode.attachChild(character1);
    collisionNode.attachChild(character2);
    
    cameraParallel();
    
    input = JoystickInput.get();
    LOG.info("Number of joysticks: " + input.getJoystickCount());
    
    final Font2D font = new Font2D();
    text = font.createText("Collisions: ", 10, 0);
    text.setLocalTranslation(new Vector3f(100, 100, 0));
    text.setRenderQueueMode(Renderer.QUEUE_ORTHO);
    ZBufferState zBufferState = 
      DisplaySystem.getDisplaySystem().getRenderer().createZBufferState();
    zBufferState.setFunction(ZBufferState.CF_ALWAYS);
    text.setRenderState(zBufferState);
    rootNode.attachChild(text);
  }
  
  protected void simpleUpdate() {
    doInput(character1, input.getDefaultJoystick().getAxisValue(4),
            input.getDefaultJoystick().getAxisValue(3));
    doInput(character2, input.getDefaultJoystick().getAxisValue(1),
            input.getDefaultJoystick().getAxisValue(0));
    
    final CollisionResults results = new BoundingCollisionResults();
    results.clear();
    character1.calculateCollisions(rootNode, results);
    text.setText("Collisions: " + results.getNumber());
  }
  
  private static void doInput(Quad character, float xAxisValue, 
                              float yAxisValue) {
      character.setLocalTranslation(
        character.getLocalTranslation().add(
          xAxisValue / 100, -yAxisValue / 100, 0));
  }
  
  public static void main(String[] args) {
    JoystickInput.setProvider(InputSystem.INPUT_SYSTEM_LWJGL);
    
    
    Test1 test1 = new Test1();
    test1.setDialogBehaviour(AbstractGame.ALWAYS_SHOW_PROPS_DIALOG);
    test1.start();
  }
}
